const http = require('fs')
/* 深度替换 */
const map = (
  target,
  callback,
  cache = new Set(),
  key = '',
  context = target
) => {
  const result = callback.call(context, target, key, context);
  if (result !== null && typeof result === 'object' && !cache.has(result)) {
    cache.add(result);
    const object = Array.isArray(result) ? [] : {};
    for (const index in result) {
      if (Object.prototype.hasOwnProperty.call(result, index)) {
        object[index] = map(result[index], callback, cache, index, result);
      }
    }
    return object;
  }
  return result;
};

// -------------------
const demo = {
  id: 0,
  self: null,
  child: {
    id: 1,
    arrt: [0, 1, 2, 3],
    self: null,
    parent: null
  }
};
http.self = http;
http.self.child = http;
map(http, (value, key, context) => {
  console.log(key, value);
  return value;
});
